package com.example.easypad;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    private List<com.example.easypad.Notes> data;
    private Context context;

    public MyAdapter(List<com.example.easypad.Notes> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View converView, ViewGroup viewGroup) {
        ViewHolder viewHolder = new ViewHolder();
        if(converView == null)
        {
            converView = LayoutInflater.from(context).inflate(R.layout.activity_watch_item,viewGroup,false);
            viewHolder.title = converView.findViewById(R.id.tv_title);
            //viewHolder.title = converView.findViewById(R.id.tv_title);
//            viewHolder.context = converView.findViewById(R.id.tv_context);
            viewHolder.type = converView.findViewById(R.id.tv_type);
            viewHolder.dateTime = converView.findViewById(R.id.tv_dateTime);
            converView.setTag(viewHolder);
        }else{

            viewHolder = (ViewHolder) converView.getTag();
        }

        String title = data.get(position).getTitle();
        if (title.length() >= 10) {
            title = title.substring(0,10) + "...";
        }
        viewHolder.title.setText(title);
//        viewHolder.context.setText(data.get(position).getContext());
        viewHolder.type.setText(data.get(position).getType());
        viewHolder.dateTime.setText(data.get(position).getDateTime());
        viewHolder.iv_menu = converView.findViewById(R.id.iv_menu);
        viewHolder.iv_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(context, "点中" + position, Toast.LENGTH_SHORT).show();
            showPopupMenu(view);
            deleteID = position;
            }
        });

        return converView;
    }
    private int deleteID;
    private SQLiteDatabase db;
    private String username, password, sql = "";
    private void deleteItem(int position) {
        String id = MainActivity.data.get(position).getId();
        db = MainActivity.db;
        sql = "DELETE FROM Notes WHERE id = " + id;
        db.execSQL(sql);
        MainActivity.data.remove(position);
        MainActivity.myAdapter = new MyAdapter(MainActivity.data,MainActivity.mainContext);
        MainActivity.lv_notes.setAdapter(MainActivity.myAdapter);

    }

    private void showPopupMenu(View v){
        //定义PopupMenu对象
        PopupMenu popupMenu = new PopupMenu(context, v);
        //设置PopupMenu对象的布局
        popupMenu.getMenuInflater().inflate(R.menu.deletemenu, popupMenu.getMenu());
        //设置PopupMenu的点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch(item.getItemId()){
                    case R.id.action_delete:{
                        deleteItem(deleteID);
                        Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
                return true;
            }
        });
        //显示菜单
        popupMenu.show();
    }

}

class ViewHolder{
    public TextView title;
    public TextView context;
    public TextView type;
    public TextView dateTime;
    public ImageView iv_menu;

}
